﻿#include "serialport.h"
#include "ui_serialport.h"
/*--------user------------*/
//portInfo
#include <QSerialPortInfo>
/*------------------------*/
SerialPort::SerialPort(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::SerialPort)
{
    ui->setupUi(this);
    //user
    system_init();
}

SerialPort::~SerialPort()
{
    delete ui;
}
/*---------------------------------------------------------------
 *              functions
 * ---------------------------------------------------------------*/
void SerialPort::system_init()
{
    btn_Detection_port();
    //port config
    global_port.setParity(QSerialPort::NoParity);///不设置校验位
    global_port.setDataBits(QSerialPort::Data8);//8bit 1字节
    global_port.setStopBits(QSerialPort::OneStop);//设置一个停止位

    //connect
    connect(ui->btn_Open,&QPushButton::clicked,this,&SerialPort::btn_open_port);
    connect(ui->btn_Detection,&QPushButton::clicked,this,&SerialPort::btn_Detection_port);
    connect(ui->btn_send,&QPushButton::clicked,this,&SerialPort::btn_send);
    connect(&global_port,&QSerialPort::readyRead,this,&SerialPort::receiver_Edit);
}
/*---------------------------------------------------------------
 *              slots
 * --------------------------------------------------------------*/
void SerialPort::btn_Detection_port(bool)
{
    ui->cmd_port_name->clear();
    QStringList PortInfoList;
    QStringList ItemInfoList;
    foreach (const QSerialPortInfo info,QSerialPortInfo::availablePorts())
    {
        PortInfoList.append(info.portName());
    }
    for(int index=0;index<ui->cmd_port_name->count();index++)
    {
        QString itemText = ui->cmd_port_name->itemText( index );
        if(!PortInfoList.contains( itemText ))
        {
            ui->cmd_port_name->removeItem(index);
        }else{
            ItemInfoList.append(itemText);
        }
    }
    foreach (QString var, PortInfoList) {
        if(!ItemInfoList.contains(var))
        {
            ui->cmd_port_name->addItem(var);
        }
    }
}
//连接串口
void SerialPort::btn_open_port(bool ok)
{
    ui->cmd_port_BaudRate->setEnabled(!ok);
    ui->cmd_port_name->setEnabled(!ok);
if(ok){
    ui->btn_Open->setText(tr("关闭串口"));
    /*-----------------------设置串口名-----------------------------------*/
    global_port.setPortName(ui->cmd_port_name->currentText());
    /*----------------------设置波特率-----------------------------*/
    switch(ui->cmd_port_BaudRate->currentIndex()){
            case 0:
                global_port.setBaudRate(QSerialPort::Baud115200);
                 break;
            case 1:
                global_port.setBaudRate(QSerialPort::Baud57600);
                 break;
            case 2:
                global_port.setBaudRate(QSerialPort::Baud38400);
                 break;
            case 3:
                global_port.setBaudRate(QSerialPort::Baud19200);
                 break;
             case 4:
                global_port.setBaudRate(QSerialPort::Baud9600);
                break;
            case 5:
              global_port.setBaudRate(QSerialPort::Baud4800);
                 break;
            case 6:
                global_port.setBaudRate(QSerialPort::Baud2400);
                break;
            case 7:
                global_port.setBaudRate(QSerialPort::Baud1200);
                break;
        }
        global_port.open(QIODevice::ReadWrite);
    }else{
    ui->btn_Open->setText(tr("打开串口"));
        global_port.close();
        ui->receive_Edit->clear();
    }
}
//发送数据
void SerialPort::btn_send(){
    QString data;
    QByteArray array;
    if(ui->hex_send->isChecked()){
        data =ui->send_lineEdit->text();
        array=data.toLatin1().toHex(' ');
    }else{
        data=ui->send_lineEdit->text();
        array=data.toLatin1();
    }
    global_port.write(array);
}
//接收数据
void SerialPort::receiver_Edit() {
    if(ui->hex_recrive->isChecked()){
        QByteArray array=global_port.readAll().toHex(' ').toUpper();
        ui->receive_Edit->appendPlainText( QString( array ) );
    }else {
        QByteArray array=global_port.readAll();
        ui->receive_Edit->appendPlainText( QString( array ) );
    }
}
